package com.pbc.gmp.action;

import com.pbc.gmp.commons.base.BaseController;
import com.pbc.gmp.commons.utils.PageInfo;
import com.pbc.gmp.model.basemodel.Insfrom;
import com.pbc.gmp.model.basemodel.Inspectiondetail;
import com.pbc.gmp.model.mybatisDto.InspectionDetailInfo;
import com.pbc.gmp.model.mybatisDto.insfrominfo;
import com.pbc.gmp.service.IInsfromService;
import com.pbc.gmp.service.IInspectiondetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *   前端控制器
 * </p>
 *
 * @author kjk
 * @since 2018-06-14
 */
@Controller
@RequestMapping("/insfrom")
public class InsfromController extends BaseController {
    @Autowired
    private IInsfromService insfromService;
    @Autowired
    private IInspectiondetailService inspectiondetailService;

    @RequestMapping(value="/insfromManger")
    public String insfromManger() {

        return "service/inspection/insfromManage";
    }
    @RequestMapping(value="/insfromList")
    @ResponseBody
    public Object indsList(insfrominfo inWhere, Integer page, Integer rows, String sort, String order) {
        PageInfo pageInfo = new PageInfo(page, rows, sort, order);
        Map<String, Object> condition = new HashMap<String, Object>();
        if (inWhere == null) {
            List<Insfrom> list=new ArrayList<Insfrom>();
            pageInfo.setRows(list);
        }else {

            if (inWhere.getOrgid() != null) {
                condition.put("inforg", inWhere.getOrgid());
            }
            if (inWhere.getDepartmentid() != null) {
                condition.put("infdep", inWhere.getDepartmentid());
            }
            if (inWhere.getEqtypeid() != null) {
                condition.put("eqtypeid", inWhere.getEqtypeid());
            }

            pageInfo.setCondition(condition);
            insfromService.selectinsfromByDetailWhere(pageInfo);
        }


        return pageInfo;

    }
    @RequestMapping(value="/addPage")
    public String addPage(Model model,@RequestParam Long eqtypeid,@RequestParam Long eqorgid,@RequestParam Long departmentid) {
        model.addAttribute("eqtypeid",eqtypeid);
        model.addAttribute("eqorgid",eqorgid);
        model.addAttribute("departmentid",departmentid);
        return "service/inspection/insfromEdit";
    }
    @RequestMapping(value="/editPage")
    public String editPage(Model model, @RequestParam Long id) {

        Insfrom Insfrom    = insfromService.selectById(id);
        model.addAttribute("infrom",Insfrom );


        return "service/inspection/insfromEdit";

    }
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object add(Insfrom inf) {
        if (inf.getId() == null) {
            inf.setId(0L);
            insfromService.insert(inf);
        }else {
            insfromService.updateById(inf);
        }

        return renderSuccess("更新成功");
    }
    @RequestMapping(value = "/delete", method = {RequestMethod.POST })
    @ResponseBody
    public Object delete(String ids) {
        List<Long > idlist= Arrays.asList(ids.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());

        insfromService.deleteBatchIds(idlist);
        return renderSuccess("删除成功！");
    }
    @RequestMapping(value="/insfromListByWhere")
    @ResponseBody
    public List<InspectionDetailInfo> insfromListByWhere(@RequestParam Long eqtypeid, @RequestParam Long eqorgid, @RequestParam Long departmentid){
        Map<String, Object> condition = new HashMap<String, Object>();
        condition.put("eqtypeid", eqtypeid);
        condition.put("eqorgid", eqorgid);
        condition.put("departmentid", departmentid);
        List<InspectionDetailInfo>list= inspectiondetailService.selectinsdetailByIds(condition);
        return list;
    }
}
